﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_PicEdit : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            string str3 = str;
            this.PicLink.NavigateUrl = "Pic.aspx?menuId=" + str3;
            this.MenuName.Text = str2;
            string str4 = base.Request.QueryString["PicId"];
            if (str4 == null)
            {
                this.ShowPicId.Text = "→ <a href=\"PicList.aspx\">请选择需编辑的图文</a>";
                this.Msg.Text = "→ <a href=\"PicList.aspx\">请选择需编辑的图文</a>";
                this.Msg.CssClass = "errorMsg";
                this.Msg.Visible = true;
                this.Edit.Enabled = false;
            }
            else
            {
                IDb db = new DbFactory().DriverDb();
                db.Open();
                DataTable oldTable = db.DataTable("ClassName,ParentId,PicClassId", "PicClass", string.Format("MenuId={0}", str3), "OrderId ASC");
                oldTable.Columns.Add("ClassNameText");
                DataTable newTable = oldTable.Clone();
                Function.PopulateTable(oldTable, newTable, "ClassName", "PicClassId", "ParentId", 0, 0);
                this.PicClassIdList.DataSource = newTable;
                this.PicClassIdList.DataTextField = "ClassNameText";
                this.PicClassIdList.DataValueField = "PicClassId";
                this.PicClassIdList.DataBind();
                DataRow row = db.TopRow("Title,PicClassId,PicId,PicContent,PicShortContent,PicPathSmall,PicPathBig,AddTime,weight,keywords", "Pic", string.Format("PicId={0}", str4), "");
                db.Close();
                this.ShowPicId.Text = (string)row["PicId"];
                this.PicTitle.Text = (string)row["Title"];
                this.PicShortContent.Text = (string)row["PicShortContent"];
                this.PicContent.Text = (string)row["PicContent"];
                this.AddTime.Text = (string)row["AddTime"];
                this.PicClassIdList.SelectedValue = (string)row["PicClassId"];
                this.PicSmallHyperLink.NavigateUrl = (string)row["PicPathSmall"];
                this.txtkeys.Text = row["keywords"].ToString();
                this.txtorder.Text = row["weight"].ToString();
                this.Edit.Enabled = true;
                if (!ManagerLogin.ValidateRole(string.Format("2_{0}_a", this.PicClassIdList.SelectedValue)))
                {
                    this.Msg.CssClass = "errorMsg";
                    this.Msg.Text = "您无此权限";
                    this.Msg.Visible = true;
                    this.Edit.Enabled = false;
                }
            }
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void Edit_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            string str = (string)this.Session["MenuId"];
            string text1 = (string)this.Session["FileName"];
            string text2 = (string)this.Session["MenuName"];
            if (str == null)
            {
                ManagerLogin.Out();
            }
            else
            {
                string str2 = str;
                if (this.PicSmall.HasFile)
                {
                    string path = string.Format("{0}{1}/Small/{2}", FSO.GetUploadPath(), str2, FSO.RandomFileName(this.PicSmall.FileName));
                    string filePath = base.Server.MapPath(path);
                    FSO.PathExists(filePath);
                    this.PicSmall.SaveAs(filePath);
                    this.PicSmallHyperLink.NavigateUrl = path;
                }

                string[,] includeColumns = new string[,] { 
                    { "PicPathSmall", this.PicSmallHyperLink.NavigateUrl, "NvarChar", "0" }, 
                    { "PicPathBig", this.PicSmallHyperLink.NavigateUrl, "NVarChar", "0" }, 
                    { "Title", this.PicTitle.Text.ToString(), "NVarChar", "0" }, 
                    { "PicShortContent", this.PicShortContent.Text.ToString(), "NVarChar", "0" }, 
                    { "AddTime", this.AddTime.Text.ToString(), "DateTime", "0" }, 
                    { "PicClassId", this.PicClassIdList.SelectedValue.ToString(), "int", "0" }, 
                    { "PicContent", this.PicContent.Text.ToString(), "NText", "0" },
                    { "Weight", this.txtorder.Text,"int","32" },
                    { "keywords", txtkeys.Text.ToString(), "NVarChar", "0" }
                };

                IDb db = new DbFactory().DriverDb();
                db.Open();
                db.Update("Pic", includeColumns, string.Format("PicId ={0}", this.ShowPicId.Text));
                db.Close();
                this.Msg.Text = "图文更新成功！";
                this.Msg.Visible = true;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.CssClass = "errorMsg";
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
        else
        {
            this.Msg.CssClass = "msg";
        }
    }


    protected void PicSmallCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.PicSmall.HasFile)
        {
            string extension = FSO.GetExtension(this.PicSmall.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.PicSmallCustomValidator.ErrorMessage = "小图格式错误，合法格式：" + extensionList;
        }
        else
        {
            this.PicSmallCustomValidator.ErrorMessage = "小图路径选择错误！";
            args.IsValid = false;
        }
    }
}
